package com.xitang.system.controller;

import com.xitang.common.dto.PageQueryRsp;
import com.xitang.common.dto.ProductQueryReq;
import com.xitang.common.dto.ResultVO;
import com.xitang.common.bean.BizProduct;
import com.xitang.common.bean.BizProductDetail;
import com.xitang.system.service.ISystemProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "产品接口列表-系统")
@RestController
public class ProductController extends BaseSystemController {
    @Autowired
    private ISystemProductService productService;

    @Value("${qiniu.domain}")
    private String domain;

    @ApiOperation(value = "获取产品列表信息", notes = "获取产品列表信息")
    @GetMapping("/product")
    public ResultVO<List<BizProduct>> productList(@RequestParam(name = "page", required = false) Integer page,
                                                  @RequestParam(name = "limit", required = false) Integer limit,
                                                  @RequestParam(name = "categoryCode", required = false) String categoryCode,
                                                  @RequestParam(name = "productCode", required = false) String productCode,
                                                  @RequestParam(name = "productName", required = false) String productName) {
        ProductQueryReq queryReq = ProductQueryReq.builder().pageNo(page).pageSize(limit).categoryCode(categoryCode)
            .productCode(productCode).productName(productName).build();
        PageQueryRsp<BizProduct> pageQueryRsp = productService.getProductList(queryReq);
        return ResultVO.success(pageQueryRsp.getRecords(), pageQueryRsp.getTotal());
    }

    @ApiOperation(value = "获取产品详细信息", notes = "根据产品编码获取产品详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "productCode", value = "产品编码", dataType = "String")
    })
    @GetMapping("/product/{productCode}")
    public ResultVO<BizProductDetail> productDetail(@PathVariable("productCode") String productCode) {
        Map<String, Object> headers = new HashMap<>();
        headers.put("domain", domain);
        return ResultVO.success(productService.getProductDetail(productCode), headers);
    }

    @ApiOperation(value = "新增产品详细信息", notes = "新增产品详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "product", value = "产品详情", dataTypeClass = BizProductDetail.class)
    })
    @PostMapping("/product")
    public ResultVO<String> addProductDetail(@RequestBody BizProductDetail product) {
        productService.saveProductDetail(product);
        return ResultVO.success(product.getProductCode());
    }

    @ApiOperation(value = "更新产品详细信息", notes = "根据产品编码更新产品详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "productCode", value = "产品编码", dataType = "String"),
            @ApiImplicitParam(paramType = "body", name = "product", value = "产品详情", dataTypeClass = BizProductDetail.class)
    })
    @PutMapping("/product/{productCode}")
    public ResultVO<BizProductDetail> updateProductDetail(@PathVariable("productCode") String productCode, @RequestBody BizProductDetail product) throws Exception {
        if (StringUtils.isEmpty(productCode)) {
            throw new Exception("productCode is empty");
        }
        productService.updateProductDetail(product);
        return ResultVO.success(null);
    }

    @ApiOperation(value = "删除产品信息", notes = "根据产品编码删除产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "productCode", value = "产品编码", dataType = "String")
    })
    @DeleteMapping("/product/{productCode}")
    public ResultVO<String> deleteProductDetail(@PathVariable("productCode") String productCode) throws Exception {
        if (StringUtils.isEmpty(productCode)) {
            throw new Exception("productCode is empty");
        }
        productService.deleteProduct(productCode);
        return ResultVO.success(productCode);
    }

    @ApiOperation(value = "导入分类产品材料信息", notes = "导入分类产品材料信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "file", value = "分类产品材料信息文件", dataTypeClass = File.class)
    })
    @PostMapping("/product/import")
    public ResultVO<String> handleFileUpload(@RequestParam("file") MultipartFile multipartFile) {
        try {
            System.out.println("Received file: " + multipartFile.getOriginalFilename());

            File tempFile = File.createTempFile(String.valueOf(System.currentTimeMillis()), ".tmp");
            multipartFile.transferTo(tempFile);

            productService.importProduct(tempFile);

            tempFile.deleteOnExit();

            return ResultVO.success(multipartFile.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
            return ResultVO.error(multipartFile.getOriginalFilename());
        }
    }
}
